package com.huoniu.user.utils;

import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.exception.CosClientException;
import com.qcloud.cos.exception.CosServiceException;
import com.qcloud.cos.model.*;
import com.qcloud.cos.region.Region;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

/**
 * @类 名： COSStorage <br/>
 * @描 述： <br/>
 * @日 期： 2020/9/20 15:50<br/>
 * @作 者： 侯建军<br/>
 * @版 本： 1.0.0
 * @since JDK 1.8
 */
public class COSStorage {
    private COSClient cosClient;
    private String secretId = "AKIDzmKeiUlFGTt291sy2KPEqofNozxiERec";
    private String secretKey = "sUq01mGBuIQKFxOkqjOnBLDo7BzWmUyC";
    private String regionName = "ap-beijing";
    /**
     * 获取一个 COSClient对象
     */
    public COSStorage() {
        COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
// 2 设置 bucket 的区域, COS 地域的简称请参照 https://cloud.tencent.com/document/product/436/6224
// clientConfig 中包含了设置 region, https(默认 http), 超时, 代理等 set 方法, 使用可参见源码或者常见问题 Java SDK 部分。
        Region region = new Region(regionName);
        ClientConfig clientConfig = new ClientConfig(region);
// 3 生成 cos 客户端。
        cosClient = new COSClient(cred, clientConfig);
    }

    /**
     * 创建存储桶
     */
    public Bucket createBucket(String bucketName) {
//        String bucket = "examplebucket-1250000000"; //存储桶名称，格式：BucketName-APPID
        CreateBucketRequest createBucketRequest = new CreateBucketRequest(bucketName);
// 设置 bucket 的权限为 Private(私有读写), 其他可选有公有读私有写, 公有读写
        createBucketRequest.setCannedAcl(CannedAccessControlList.Private);
        try {
            return cosClient.createBucket(createBucketRequest);
        } catch (CosServiceException serverException) {
            serverException.printStackTrace();
        } catch (CosClientException clientException) {
            clientException.printStackTrace();
        }
        return null;
    }

    /**
     * 文件上传
     */
    public String upload(String path) {
        // 指定要上传到的存储桶
        String bucketName = "team-shop-1303824460";
        // 查询桶名
        List<Bucket> buckets = cosClient.listBuckets();
        boolean isBucket = false;
        for (Bucket bucketElement : buckets) {
            if (bucketName.equals(bucketElement.getName())) {
                isBucket = true;
            }
//            String bucketLocation = bucketElement.getLocation();
        }
        if (!isBucket) {
            createBucket(bucketName);
        }
        // 指定要上传到 COS 上对象键
        Date date = new Date();
        String key = "/ad/" + date.getTime() + "";
        File localFile = new File(path);
        // 请求
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, localFile);
        PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
//        https://huoniu8-1253308853.cos.ap-nanjing.myqcloud.com/0000/2020/b151f8198618367a32069ff32f26c4d3b21ce5ea.png
        String imagePath = "https://" + bucketName + ".cos" + regionName + ".myqcloud.com/" + key;
        return imagePath;
    }

    /**
     * 文件上传
     */
    public String upload(InputStream inputStream, String ext) throws IOException {
        // 指定要上传到的存储桶
        String bucketName = "team-shop-1303824460";
        // 查询桶名
        List<Bucket> buckets = cosClient.listBuckets();
        boolean isBucket = false;
        for (Bucket bucketElement : buckets) {
            if (bucketName.equals(bucketElement.getName())) {
                isBucket = true;
            }
//            String bucketLocation = bucketElement.getLocation();
        }
        if (!isBucket) {
            createBucket(bucketName);
        }
        // 指定要上传到 COS 上对象键
        Date date = new Date();
        String key = "/ad/" + date.getTime() + "."+ext;
        // 元数据
        ObjectMetadata objectMetadata = new ObjectMetadata();
        // 设置 Content type, 默认是 application/octet-stream
        objectMetadata.setContentType("image/"+ext);
        // 请求对象
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, inputStream, objectMetadata);
        // 限流使用的单位是 bit/s, 这里设置上传带宽限制为 10MB/s
        putObjectRequest.setTrafficLimit(80 * 1024 * 1024);
        // 请求
        PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);

//        https://huoniu8-1253308853.cos.ap-nanjing.myqcloud.com/0000/2020/b151f8198618367a32069ff32f26c4d3b21ce5ea.png
        String imagePath = "https://" + bucketName + ".cos." + regionName + ".myqcloud.com" + key;
        return imagePath;
    }
}
